package com.zlw.test;

import com.zlw.test.bean.MyBean;
import com.zlw.test.config.MyInfo;
import com.zlw.test.service.GoodsService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

/**
 * @author zhaoluowei
 * @description TODO springboot 默认是 logback日志文件
 * @return
 * @date 2021/1/22 13:59
 */
@Slf4j
@Import({MyBean.class,})
//@ServletComponentScan(basePackages = {
//        "com.zlw.test.servlet",
//        "com.zlw.test.filter",
//        "com.zlw.test.listener"
//})
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer implements CommandLineRunner {


    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${my.asd}")
    private String asd;

    @Value("${my.asd_uuid}")
    private String asd_uuid;


    @Resource
    private MyInfo myInfo;


    public static void main(String[] args) {
        //启动方式一
        //SpringApplication.run(DemoApplication.class, args);

        //启动方式二
        ConfigurableApplicationContext context = new SpringApplicationBuilder()
                .sources(DemoApplication.class)
                .bannerMode(Banner.Mode.CONSOLE)
                .run(args);

        //service -> mapper 获取数据
        GoodsService goodsService = context.getBean(GoodsService.class);
        System.out.println("goodsService.selectByPrimaryKey(1) = " + goodsService.selectByPrimaryKey(1));

        MyBean bean = context.getBean(MyBean.class);
        System.out.println("bean = " + bean);


    }

    /**
     * @param args
     * @return void
     * @description TODO springIOC 启动成功之后 会回调这个方法
     * @author zhaoluowei
     * @date 2021/1/22 10:49
     */
    @Override
    public void run(String... args) throws Exception {
        log.info("[回调执行] ... 缓存配置文件信息 -> {} ",applicationName);
        log.info("[回调执行] ... 获取配置文件asd -> {} ",asd);
        log.info("[回调执行] ... 获取配置文件asd_uuid -> {} ",asd_uuid);
        log.info("[回调执行] ... 获取配置文件myInfo -> {} ",myInfo);
    }


    /**
     * @description 项目 打war包
     * @param application
     * @return org.springframework.boot.builder.SpringApplicationBuilder
     * @author zhaoluowei
     * @date 2021/1/26 22:07
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

}
